package com.lijinjiang.jdbc;

import java.sql.*;

/**
 * @ClassName Demo02
 * @Description TODO
 * @Author Li
 * @Date 2022/9/22 8:54
 * @ModifyDate 2022/9/22 8:54
 * @Version 1.0
 */
public class Demo02 {
    public static void main(String[] args) throws Exception {

        // 1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.获取连接对象
        String username = "root";
        String password = "123456";
        String url = "jdbc:mysql://localhost:3306/db01?useSSL=true";
        Connection conn = DriverManager.getConnection(url, username, password);
        // 3.定义 SQL
        String sql = "SELECT * FROM tb_user WHERE username = ? and password = ?";
        // 4.获取执行 SQL 的对象
        PreparedStatement prepStmt = conn.prepareStatement(sql);
        prepStmt.setString(1, "古天乐");
        prepStmt.setString(2, "123456");
        // 5.执行 SQL
        ResultSet rs = prepStmt.executeQuery();
        // 6.处理结果
        while (rs.next()) {
            String id = rs.getString("id");
            String name = rs.getString("username");
            String gender = rs.getString("gender");
            String address = rs.getString("address");
            System.out.println("[" + id + "," + name + "," + gender + "," + address + "]");
        }
        // 7.释放资源
        rs.close();
        prepStmt.close();
        conn.close();

    }
}
